package com.healer.controller;

import com.healer.common.AssembleResponseMsg;
import com.healer.pojo.Book;
import com.healer.pojo.ReturnBody;
import com.healer.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {
    @Autowired
    BookService bookService;

    /**
     * 功能描述：添加图书
     * @param map
     * @return
     */
    @RequestMapping("/addbook")
    public ReturnBody addBook(@RequestBody Map<String,Object> map){
        try {
            bookService.addBook(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书添加失败");
        }
    }

        /**
     * 功能描述：修改图书
     * @param map
     * @return
     */
    @RequestMapping("/editbook")
    public ReturnBody updateBook(@RequestBody Map<String,Object> map){
        try {
            bookService.editBook(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书修改失败");
        }
    }
    /**
     * 功能描述：删除图书
     * @param map
     * @return
     */
    @RequestMapping("/deletebook")
    public ReturnBody deleteBook(@RequestBody Map<String,Object> map){
        try {
            bookService.deleteBook(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书修改失败");
        }
    }
    /**
     * 功能描述：查询图书
     * @param map
     * @return
     */
    @RequestMapping("/querybooklist")
    public ReturnBody queryBookList(@RequestBody Map<String,Object> map){
        try {
            Map<String,Object> map1= bookService.queryBookList(map);
            return new AssembleResponseMsg().success(map1);
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书查询失败");
        }
    }

//图书借阅
    /**
     * 功能描述：图书借阅
     * @param map
     * @return
     */
    @RequestMapping("/addborrowingbook")
    public ReturnBody addBorrowingBook(@RequestBody Map<String,Object> map){
        try {
           bookService.addBorrowingBook(map);
            Map<String,Object> map1=new HashMap<>();
            map1.put("bookId",map.get("bookId"));
            map1.put("inventory",-1);
            bookService.updateInventories(map1);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书借阅失败");
        }
    }
    //图书归还
    /**
     * 功能描述：图书归还
     * @param map
     * @return
     */
    @RequestMapping("/returnborrowingbook")
    public ReturnBody returnBorrowingBook(@RequestBody Map<String,Object> map){
        try {
            if(map.get("returnStatus").equals("已归还"));
           bookService.returnBorrowingBook(map);
           Map<String,Object> map1=new HashMap<>();
           map1.put("bookId",map.get("bookId"));
           map1.put("inventory",1);
           bookService.updateInventories(map1);
            return new AssembleResponseMsg().success("OK");

        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书归还失败");
        }
    }
    //图书借阅查询
    /**
     * 功能描述：图书借阅查询
     * @param map
     * @return
     */
    @RequestMapping("/queryborrowing")
    public ReturnBody queryBorrowing(@RequestBody Map<String,Object> map){
        try {
            Map<String,Object> map1 = bookService.queryBorrowing(map);
            return new AssembleResponseMsg().success(map1);
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书借阅查询失败");
        }
    }

        //图书库存更新
    /**
     * 功能描述：图书库存更新
     * @param map
     * @return
     */
    @RequestMapping("/updateinventory")
    public ReturnBody updateInventory(@RequestBody Map<String,Object> map){
        try {
         bookService.updateInventories(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"error","图书库存更新失败");
        }
    }



}
